<?php
declare (strict_types = 1);

namespace app\admin\controller\message;

use app\admin\BaseController;
use think\Exception;
use think\facade\Log;
use app\common\model\message\Comment as CommentModel;

class Comment extends BaseController
{
    //是否关联user模型( 用于权限规则验证 )
    public $relateUser = true;

    //中间件
    protected $middleware = [
        'method:ajax' => ['only'=>['list','update','delete','batchDel']],
    ];

    /**
     * 显示资源列表
     *
     * @return \think\Response
     */
    public function index()
    {
        return view('index');
    }

    //列表数据
    public function list(){
        try{
            //获取列表数据
            $data = CommentModel::getListData($this->ruleWhere());


        }catch (\Exception $e){
            //系统错误
            //Log::error($e->getMessage());
            return json([
                'code' => 1,
                'msg' => $e->getMessage(),
            ]);
        }

        //成功返回
        return json([
            'code'  => 0,
            'msg'=> '查询列表成功',
            'count' => $data['count'],
            'data'  => $data['data']
        ]);

    }

    /**
     * 评论详情
     * @param int $id
     */
    public function read(int $id){
        return view('read',[
            'model' => $this->getModel(CommentModel::class, $id,['user'])
        ]);
    }


    /**
     * 显示编辑资源表单页.
     *
     * @return \think\Response
     */
    public function edit(int $id)
    {
        return view('edit',[
            'model'=>$this->getModel(CommentModel::class, $id)
        ]);
    }

    /**
     * 保存更新的资源
     *
     * @param  \think\Request  $request
     * @param  int  $id
     * @return \think\Response
     */
    public function update(int $id)
    {
        try{
            //获取内容
            $comment = input('comment/s');
            if (empty($comment)) throw new Exception('内容不能为空.');

            $ret = $this->getModel(CommentModel::class, $id)->save([
                'comment' => $comment
            ]);
            if ($ret === false)
                throw new Exception('修改失败.');

            return json([
                'code' => 0,
                'msg' => '修改成功.'
            ]);


        }catch (\Exception $e){
            //Log::error($e->getMessage());
            return json([
                'code' => 1,
                'msg' => $e->getMessage()
            ]);
        }
    }

    /**
     * 删除指定资源
     * @return \think\response\Json
     * @throws Exception
     */
    public function delete(int $id)
    {
        //获取参数
        $model = $this->getModel(CommentModel::class, $id);
        try{

            if ($model->delete() === false){
                throw new Exception('删除评论失败.');
            }

        }catch (\Exception $e){

            //Log::error($e->getMessage());
            return json(['code'=>1,'msg'=>$e->getMessage()]);
        }
        //删除成功
        return json(['code'=>0,'msg'=>'删除评论成功']);
    }

    //批量删除
    public function batchDel(){
        try{
            $ids = explode(',',trim(request()->param('ids'),','));

            if( $this->batchRule(CommentModel::class, (array) $ids) &&
                CommentModel::destroy($ids) === false )
            {
                throw new Exception('批量删除评论失败.');
            }

        }catch (\Exception $e){
            //Log::error($e->getMessage());
            return json([
                'code' => 1,
                'msg' => $e->getMessage()
            ]);
        }

        return json([
            'code' => 0,
            'msg' => '批量删除评论成功.'
        ]);

    }
}
